package com.shop.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.admin.constant.WechatModuleConstant;
import com.shop.admin.dao.WechatModuleDao;
import com.shop.admin.entity.WechatModule;
import com.shop.admin.service.WechatModuleService;
import com.shop.admin.utils.PageUtils;
import com.shop.admin.utils.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class WechatModuleServiceImpl extends ServiceImpl<WechatModuleDao, WechatModule> implements WechatModuleService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WechatModule> page = this.page(
                new Query<WechatModule>().getPage(params),
                new QueryWrapper<WechatModule>()
                        .lambda()
                        .eq(WechatModule::getOrgId,params.get("orgId"))
                        .eq(WechatModule::getIsDeleted, WechatModuleConstant.StatusEnum.NO_DELETED.getCode())
        );
        return new PageUtils(page);
    }

    @Override
    public void updateOrSaveWechatModule(WechatModule wechatModule) {
        if (wechatModule.getId()==null){
            WechatModule wechatModuleDB = new WechatModule();
            BeanUtils.copyProperties(wechatModule,wechatModuleDB);
            wechatModuleDB.setCreationTime(new Date());
            this.save(wechatModuleDB);
        }else {
            this.update(wechatModule,null);
        }

    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
        for (Long aLong : asList) {
            WechatModule wechatModule = new WechatModule();
            wechatModule.setId(aLong.intValue());
            wechatModule.setIsDeleted(WechatModuleConstant.StatusEnum.IS_DELETED.getCode());
            wechatModule.setStatus(WechatModuleConstant.StatusEnum.STATUS_DONT_DISPLAY.getCode());
            this.updateById(wechatModule);
        }
    }
}
